package com.zhangzhan.gulimallware.controller;

import com.zhangzhan.gulimallcommcon.tos.SkuCheckInventoryTo;
import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.R;
import com.zhangzhan.gulimallware.entity.WareSkuEntity;
import com.zhangzhan.gulimallware.service.WareSkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.zhangzhan.gulimallcommcon.exception.exception_enum.ExceptionEnum.UNNOW_EXCEPTION;
import static com.zhangzhan.gulimallcommcon.exception.exception_enum.ExceptionEnum.VALID_WARE_SKU_EXCEPTION;


/**
 * 商品库存
 *
 * @author zhangzhan
 * @email sunlightcs@gmail.com
 * @date 2024-06-18 23:32:09
 */
@RestController
@RequestMapping("gulimallware/waresku")
@Slf4j
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    /**
     * 查询库存
     */
    @RequestMapping(value = "/check/inventory", method = RequestMethod.POST)
    //@RequiresPermissions("gulimallware:waresku:list")
    public List<SkuCheckInventoryTo> checkInventory(@RequestBody List<Long> skuIds) {
        return wareSkuService.checkInventory(skuIds);
    }


    /**
     * 锁定库存
     */
    @RequestMapping(value = "/lockInventory", method = RequestMethod.POST)
    //@RequiresPermissions("gulimallware:waresku:list")
    public R lockInventory(@RequestBody Map<Long, Integer> map, @RequestParam("orderSn") String orderSn) {
        try {
            boolean res = wareSkuService.lockInventory(map, orderSn);
            if (res) {
                return R.ok();
            } else {
                log.error("库存锁定失败。。。。状态码：{}，信息：{}", UNNOW_EXCEPTION.getCode(), UNNOW_EXCEPTION.getMeg());
                return R.error();
            }
        } catch (Exception e) {
            log.error("库存锁定失败。。。。状态码：{}，信息：{}", VALID_WARE_SKU_EXCEPTION.getCode(), e.getMessage());
            return R.error();
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimallware:waresku:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareSkuService.queryPageDetail(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gulimallware:waresku:info")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimallware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimallware:waresku:delete")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
